package com.tcc.task;

import org.apache.commons.io.FileUtils;
import org.apache.xpath.operations.String;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.tcc.qa.test.util.BaseTest;
import com.tcc.qa.test.util.ButtonFinder;
import com.tcc.qa.test.util.DatePicker;
import com.tcc.qa.test.util.FileDataSource;
import com.tcc.qa.test.util.InputFinder;
import com.tcc.qa.test.util.SelectFinder;
import com.tcc.qa.test.util.SwitchTo;
import com.tcc.qa.test.util.TableCellFinder;

@SuppressWarnings("unused")
public class CreateTask extends BaseTest {
	String categoryName = "Study";
	String notes = "Selenium automates browsers";
	@Test(description="Test whether the alert will appear when title is null.", priority=1)
	public void titleIsNull(){
		ButtonFinder.getButtonById(getWebDriver(), "qbtasks").click();
		ButtonFinder.getTableButton(getWebDriver(), "New Task").click();
		DatePicker.selectToday(getWebDriver());
		getWebDriver().findElement(By.id("ccEditorrtfDetail")).sendKeys(notes);
		ButtonFinder.getSubmitButtonByValue(getWebDriver(),"Save").click();
		Assert.assertTrue(getWebDriver().switchTo().alert().getText().contains("Title"));
		getWebDriver().switchTo().alert().accept();
	}
	@Test(description="Create and check Task1", priority=2, groups={"Test1"})
	public void Task1() throws IOException{
		InputFinder.getInputById(getWebDriver(), "mainForm", "title").sendKeys("Task1");
		SelectFinder.getSelectOption(getWebDriver(), "statusId", 0).click();
		DatePicker.selectPreDay(getWebDriver());
		String dueDate = InputFinder.getInputById(getWebDriver(), "dueDate")
				.getAttribute("value");
		InputFinder.getInputById(getWebDriver(), "linkToCal").click();
		getWebDriver().findElement(By.id("ccEditorrtfDetail")).sendKeys(notes); //iframe
		// Used to upload picture.
		getWebDriver().findElement(By.xpath("//*[@id='ccEditorrtfDetailImage']/img")).click();
		getWebDriver().findElement(By.id("ccEditorUploadMsgImage")).click();
		for(String handle : getWebDriver().getWindowHandles()){
			 getWebDriver().switchTo().window(handle);
		}
		InputFinder.getInputById(getWebDriver(), "imageText").sendKeys("This is a png picture");
		ButtonFinder.getInputButtonByValue(getWebDriver(), "Upload").click();
		Assert.assertEquals(getWebDriver().switchTo().alert().getText(), "Please select an image to upload!");
		getWebDriver().switchTo().alert().accept();
		File picFile= new File("C:/Users/Administrator.zxcv-PC/Desktop/QQ20120518134719.png");
		InputFinder.getInputById(getWebDriver(), "fileName").sendKeys(picFile.getAbsolutePath());
		ButtonFinder.getInputButtonByValue(getWebDriver(), "Upload").click(); 
		ButtonFinder.getSubmitButtonByValue(getWebDriver(), "Apply").click();
		String windowId="";
		for(String handle : getWebDriver().getWindowHandles()){
			if(getWebDriver().switchTo().window(handle).getTitle().contains("Task Editor")){
				windowId=handle;
				break;
			}
			getWebDriver().switchTo().window(windowId);
		}
		ButtonFinder.getSubmitButtonByValue(getWebDriver(), "Save").click();
		@SuppressWarnings("deprecation")
		Date newDate=new Date(dueDate);
		Locale locale=new Locale("en","US");
		DateFormat dateFormat=new SimpleDateFormat("EE, MMM dd, yyyy", locale);
		String dateString=dateFormat.format(newDate);
		Assert.assertTrue(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 2).endsWith("Task1"));
		Assert.assertEquals(getWebDriver().findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]/a/span")).getAttribute("class"),"ccAlert");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 3), "1");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 4), dateString);
		Assert.assertEquals(getWebDriver().findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[4]/span")).getAttribute("class"),"ccAlert");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 5), "New");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 6), "");
		getWebDriver().findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]/a/span")).click();
		File screenshot=((TakesScreenshot)getWebDriver()).getScreenshotAs(OutputType.FILE);
		FileUtils.copyFile(screenshot, new File("D:\\TestResult\\screenshot.png"));
		ButtonFinder.getButtonById(getWebDriver(), "qbcalendar").click();
		getWebDriver().findElement(By.id("prevBtn")).click();  //<button id="prevBtn" title="Previous Month" onclick="doCommand('load&dt=20120423')" type="button">
		Assert.assertEquals(getWebDriver().findElement(By.xpath("//tbody[@id='mTbody']/tr[2]/td[2]/div[2]")).getAttribute("class"),"eventList"); //tbody?
	
	}
	@Test(description="Create and check Task2", priority=3)
	public void Task2(){
		ButtonFinder.getButtonById(getWebDriver(), "qbtasks").click();
		ButtonFinder.getTableButton(getWebDriver(), "New Task").click();
		InputFinder.getInputById(getWebDriver(), "mainForm", "title").sendKeys("Task2");
		SelectFinder.getSelectById(getWebDriver(), "category").selectByVisibleText(categoryName);
        SelectFinder.getSelectOption(getWebDriver(), "priority", 2).click();
        DatePicker.selectToday(getWebDriver());      
        SelectFinder.getSelectOption(getWebDriver(), "statusId",1).click();
		InputFinder.getInputById(getWebDriver(), "ccEditorrtfDetailImage").click();
		InputFinder.getInputById(getWebDriver(), "ccEditorUploadMsgImage").click();
		SwitchTo.getWindowHandle(getWebDriver());
		File picFile= new File("C:/Users/Administrator.zxcv-PC/Desktop/QQ20120518134719.png");
		InputFinder.getInputById(getWebDriver(), "fileName").sendKeys(picFile.getAbsolutePath());
		InputFinder.getInputById(getWebDriver(), "imageText").sendKeys("This is a png picture");
		ButtonFinder.getInputButtonByValue(getWebDriver(), "Upload").click(); //upload button
		ButtonFinder.getSubmitButtonByValue(getWebDriver(), "Apply").click();//apply button
		SwitchTo.getWindowHandle(getWebDriver());
		String dueDate=InputFinder.getInputById(getWebDriver(), "dueDate").getAttribute("value");
		ButtonFinder.getSubmitButtonByValue(getWebDriver(), "Save").click();
		@SuppressWarnings("deprecation")
		Date newDate=new Date(dueDate);
		Locale locale=new Locale("en","US");
		DateFormat dateFormat=new SimpleDateFormat("EE, MMM dd, yyyy", locale);
		String dateString=dateFormat.format(newDate);
		Assert.assertTrue(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 2).endsWith("Task2"));
		//Assert.assertEquals(getWebDriver().findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]/a/span")).getAttribute("class"),"");//Day is not stable.
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 3), "3");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 4), dateString);
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 5), "Status3");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 6), categoryName);
		
	}
	@Test(description="Create and check Task3", priority=4)
	public void Task3() throws IOException{
		ButtonFinder.getTableButton(getWebDriver(), "New Task").click();
		InputFinder.getInputById(getWebDriver(), "mainForm", "title").sendKeys("Task3");
		SelectFinder.getSelectById(getWebDriver(), "category").selectByVisibleText(categoryName);
		SelectFinder.getSelectOption(getWebDriver(), "priority", 4).click();
    	SelectFinder.getSelectById(getWebDriver(), "statusId").selectByVisibleText("Completed");
		DatePicker.selectLaterDay(getWebDriver(),7);
		getWebDriver().findElement(By.id("ccEditorrtfDetail")).sendKeys(notes);
		String dueDate = InputFinder.getInputValueById(getWebDriver(), "dueDate");
		ButtonFinder.getSubmitButtonByValue(getWebDriver(), "Save").click();
		@SuppressWarnings("deprecation")
		Date newDate = new Date(dueDate);
		Locale locale = new Locale("en", "US");
		DateFormat dateFormat = new SimpleDateFormat("EE, MMM dd, yyyy", locale);
		String dateString = dateFormat.format(newDate);
		Assert.assertTrue(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 2).endsWith("Task2"));
	//	Assert.assertEquals(getWebDriver().findElement(By.xpath("//table[@id='taskTable']/tbody/tr[1]/td[2]/a/span")).getAttribute("class"),"");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 3), "3");
	//	Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 4), dateString);
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 5), "Status3");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 1, 6), categoryName);
		
		Assert.assertTrue(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 2, 2).endsWith("Task3"));
		Assert.assertEquals(getWebDriver().findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[2]/a/span")).getAttribute("class"),"ccStrike");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 2, 3), "5");
		Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 2, 4), dateString);
    Assert.assertEquals(getWebDriver().findElement(By.xpath("//table[@id='taskTable']/tbody/tr[2]/td[4]/span")).getAttribute("class"), "ccStrike");
    Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 2, 5), "Completed");
    Assert.assertEquals(TableCellFinder.getCellValue(getWebDriver(), "taskTable", 2, 6), categoryName);
  }

  @DataProvider
  public Object[][] dp() {
    return FileDataSource.getDatas(CreateTask.class, "tasks.xml");
  }
}
